﻿Imports Global_Visas_Business_Layer
Imports System.Text
Imports System.Data
Imports System.ComponentModel

Partial Public Class frmAddPersonToCompany
    Private blnAdvanced As Boolean = False
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAdd.Click
        If validateOK() = True Then
            ' update the records
            If CommonDataFunctions.AddPersonToCompany(txtCompanyID.Text.ToString, drpRole.SelectedValue) Then
                CommonFuncs.DisplayMessage("The person " & GlobalCRM.CurrentCandidateID & " has been added to the company id : " & txtCompanyID.Text.ToString, CommonDialog.CustomDialogIcons.Information, "Success")

                Me.Close()
            Else
                CommonFuncs.DisplayMessage("There was an error adding the person to company. Please contact IT", CommonDialog.CustomDialogIcons.Information, "Error")
                Me.Close()
            End If
        End If
    End Sub
    Private Function validateOK() As Boolean
        ' Dim sql As StringBuilder
        Dim companyname As String
        If Me.drpRole.SelectedIndex = -1 Then
            CommonFuncs.DisplayMessage("Please select the role of the client", CommonDialog.CustomDialogIcons.Information, "Role")
            drpRole.Focus()
            Return False
        End If
        If Me.txtCompanyID.Text = String.Empty Then
            CommonFuncs.DisplayMessage("Please enter the company ID", CommonDialog.CustomDialogIcons.Information, "Role")
            txtCompanyID.Focus()
            Return False
        Else
            If IsNumeric(txtCompanyID.Text) = True Then
                'sql = New StringBuilder
                companyname = CommonDataFunctions.getCompanyNameFromID(txtCompanyID.Text.ToString.Trim)

                If companyname Is Nothing Or companyname = String.Empty Then
                    CommonFuncs.DisplayMessage("The company ID is not valid. Please click on the Advanced Options to search for the company", CommonDialog.CustomDialogIcons.Information, "Search")
                    Return False
                End If
            Else
                CommonFuncs.DisplayMessage("Please enter a numeric value for company id", CommonDialog.CustomDialogIcons.Information, "Numeric value")
                Return False
            End If
        End If
        If MsgBox("Are you sure to add the person " & People.GetPersonName(GlobalCRM.CurrentCandidateID) & " to the company " & companyname & " ?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            Return True
        Else : Return False
        End If

    End Function

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSearch.Click
        Dim myData As DataTable

        If rdoName.IsChecked = True Then
            If txtCompanyIDorName.Text = String.Empty Then
                CommonFuncs.DisplayMessage("Please enter company Name or part of company name to search", CommonDialog.CustomDialogIcons.Information, "Error")
                txtCompanyIDorName.Focus()
                Exit Sub
            Else
                If txtCompanyIDorName.Text.Length >= 3 Then
                    myData = Company.SimilarCompanies(Me.txtCompanyIDorName.Text)
                Else
                    CommonFuncs.DisplayMessage("Please type in atleast 3 letters. ", CommonDialog.CustomDialogIcons.Information, "Error")
                    txtCompanyIDorName.Focus()
                    Exit Sub
                End If
            End If
        End If

        If rdoID.IsChecked = True Then
            If txtCompanyIDorName.Text = String.Empty Then
                CommonFuncs.DisplayMessage("Please enter company ID to search", CommonDialog.CustomDialogIcons.Information, "Error")
                txtCompanyIDorName.Focus()
                Exit Sub
            End If
            If IsNumeric(txtCompanyIDorName.Text) = False Then
                CommonFuncs.DisplayMessage("Please enter a numer value for ID or Please choose Company Name search to find the company", CommonDialog.CustomDialogIcons.Information, "Error")
                txtCompanyIDorName.Focus()
                Exit Sub
            End If
            myData = Company.SimilarCompaniesFromID(CInt(txtCompanyIDorName.Text))
        End If

        If Not myData Is Nothing Then
            If myData.Rows.Count > 0 Then
                DataBinding.BindListView(myData, Me.lvwSimilar)
            Else
                CommonFuncs.DisplayMessage("The selected data did not match. Please try with different option", CommonDialog.CustomDialogIcons.Information, "Error")
                Exit Sub
            End If
        End If
    End Sub
    Private Sub searchCompany()

        'Dim SQL As New StringBuilder
        'With SQL
        '        .Append("select company_id from d_company where company_name like '%" & txtCompanyIDorName.Text & "'%")
        'End With

        Dim dtbSearchResults As DataTable

        dtbSearchResults = Company.SimilarCompanies(txtCompanyIDorName.Text.Trim)
        If dtbSearchResults Is Nothing Then
            Exit Sub
        End If

        If dtbSearchResults.Rows.Count = 0 Then
            Exit Sub
        End If
        If dtbSearchResults.Rows.Count > 1000 Then
            CommonFuncs.DisplayMessage("Over 1000 matching records found. Please Refine your search for better results", CommonDialog.CustomDialogIcons.Information, "Search Results")
            Exit Sub
        End If
        DataBinding.BindListView(dtbSearchResults, lvwSimilar)

    End Sub
    Private Sub btnAdvanced_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAdvanced.Click
        If blnAdvanced = False Then
            Me.Height = 440
            Me.Width = 364
            btnAdvanced.Content = "Advanced Options <<"
            blnAdvanced = True
        Else
            Me.Height = 145
            Me.Width = 364
            btnAdvanced.Content = "Advanced Options >>"
            blnAdvanced = False
        End If
    End Sub

    Private Sub frmAddPersonToCompany_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.KeyUp

    End Sub

    Private Sub frmAddPersonToCompany_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        'Me.Content = "Add Person " & GlobalCRM.CurrentCandidateID & " to the company"
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        Me.Title = "Add Person " & GlobalCRM.CurrentCandidateID & " to the company"
        blnAdvanced = False
        Me.Height = 145
        Me.Width = 364
        txtCompanyID.Text = String.Empty
        txtCompanyIDorName.Text = String.Empty
        Me.lvwSimilar.Items.Clear()
        btnAdvanced.Content = "Advanced Options >>"

        With Me.drpRole
            .SelectedValuePath = "role_id"
            .DisplayMemberPath = "role_name"
            .ItemsSource = CType(CommonDataFunctions.RoleList, IListSource).GetList
            .SelectedIndex = -1
        End With
    End Sub

    Private Sub lvwSimilar_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles lvwSimilar.MouseDoubleClick
        Me.txtCompanyID.Text = Me.lvwSimilar.SelectedItem.row.itemarray(0).ToString
        btnAdvanced.Content = "Advanced Options >>"
        blnAdvanced = False
        Me.Height = 145
        Me.Width = 364
    End Sub
End Class
